package com.atguigu.tingshu.order.strategy.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Author  王东剑
 * Created on 21/6/2024 PM6:40
 * Package_name com.atguigu.tingshu.order.strategy.impl
 * Description
 */
@Service
@Slf4j
public class TrackOrderTradeStrategy implements OrderTradeStrategy {
	@Autowired
	private UserInfoFeignClient userInfoFeignClient;
	@Autowired
	private AlbumInfoFeignClient albumInfoFeignClient;
	@Autowired
	private TrackInfoFeignClient trackInfoFeignClient;
	@Autowired
	private StringRedisTemplate redisTemplate;

	@Override
	public OrderInfoVo trade(TradeVo tradeVo) {
		log.info("TrackOrderTradeStrategy.trade");
		Long userId = AuthContextHolder.getUserId();
		// 构造返回对象
		OrderInfoVo orderInfoVo = new OrderInfoVo();
		Long itemId = tradeVo.getItemId();
		Integer trackCount = tradeVo.getTrackCount();
		// 如果购买数量小于1,抛异常
		if (trackCount < 0)
			throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
		// 获取下单声音列表
		List<TrackInfo> trackInfoList = trackInfoFeignClient.findPaidTrackInfoList(itemId,trackCount).getData();
		// 获取专辑信息
		TrackInfo trackInfoFirst = trackInfoList.get(0);
		AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfo(trackInfoFirst.getAlbumId()).getData();
		// 计算价格
		BigDecimal originalAmount = tradeVo.getTrackCount().intValue() > 0 ?
				albumInfo.getPrice().multiply(new BigDecimal(tradeVo.getTrackCount())) :
				albumInfo.getPrice();
		orderInfoVo.setOriginalAmount(albumInfo.getPrice());
		orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
		orderInfoVo.setOrderAmount(originalAmount);
		// 构造详细列表
		orderInfoVo.setOrderDetailVoList(trackInfoList
				.stream()
				.map(trackInfo -> OrderDetailVo.builder()
						.itemId(trackInfo.getId())
						.itemName(trackInfo.getTrackTitle())
						.itemUrl(trackInfo.getCoverUrl())
						.itemPrice(albumInfo.getPrice())
						.build())
				.collect(Collectors.toList()));
		// 没有折扣
		orderInfoVo.setOrderDerateVoList(Lists.newArrayList());
		this.setOtherParameters(orderInfoVo,redisTemplate);
		return orderInfoVo;
	}

	@Override
	public String getItemType() {
		return SystemConstant.ORDER_ITEM_TYPE_TRACK;
	}
}
